$reffile_dir = $ARGV[0];
chomp($reffile_dir);
$reffile_dir =~ s/\/$//g;
$inputfile=$ARGV[1];
chomp($reffile_dir);
$outputfile=$ARGV[2];
chomp($outputfile);
$outputfile1=$ARGV[3];
chomp($outputfile1);
open(BUFF,$inputfile) or die "no file exists $inputfile\n";
open(WRBUFF1,">$outputfile") or die "not able write to $outputfile\n";
open(WRBUFF2,">$outputfile1") or die "not able write to $outputfile1\n";
$prevchr = "";
my %hash;
while(<BUFF>)
{
	#print "$.\n";
	chomp($_);
	@a = split(" ",$_);
	$flag = 0;
	if($a[0] > 23)
	{
		$flag = 1;
	}
	if($prevchr ne $a[0] && $a[0] <24)
	{
		if($a[0] == 23)
		{
			$tmp ="X";
		}
		else
		{
			$tmp=$a[0];
		}
		undef(%hash);
		$file1 = "$reffile_dir/chr".$tmp."_1K_impute_ref.txt";
		#die "$file1\n";
		open(INPUT1,$file1) or die "no file exists $file1\n";
		while($line = <INPUT1>)
		{
			chomp($line);
			@in = split("\t",$line);
			$hash{$in[0]} = $in[2];
		}
		$file2 = "$reffile_dir/chr".$tmp."_1K_nonimpute_ref.txt";
		open(INPUT1,$file2) or die "no file exists $file2\n";
		while($line = <INPUT1>)
                {
                        chomp($line);
                        @in = split("\t",$line);
                        $hash{$in[0]} = $in[2];
                }
	}
	if(exists($hash{$a[1]}) && $flag != 1)
	{
		$a[3] = $hash{$a[1]};
		$_ = join(" ",@a);
		print WRBUFF1 "$_\n";	
	}
	else
	{
		print WRBUFF2 "$_\n";
	}
	$prevchr =$a[0];
}
